<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page import="fr.univ.surveyonline.model.User" %>
<%@ page import="fr.univ.surveyonline.model.Role" %>
<%@ page import="fr.univ.surveyonline.model.Survey" %>
<%@ page import="fr.univ.surveyonline.model.ResponseManager" %>
<%@ page import="fr.univ.surveyonline.datastore.PMF" %>
<%@ page import="java.util.*" %>
<%@ page import="fr.univ.surveyonline.language.Language" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
    <title><tag:languageTag id="title"/></title>
	<%@ include file="/layout/head.tag" %>
</head>
<body>

<div id="global">
	<div id="entete">
		<%@ include file="/layout/menu.tag" %>
	</div>
	<div id="contenu">
		<h2><tag:languageTag id="observer_title"/></h2>
		<%	
			String langue = "FR";
			for (Cookie c : request.getCookies()) {
				if (c.getName().equals("langue")) {
					langue = c.getValue(); 
					break;
				}
			}
			if (request.getSession().getAttribute("user") == null) {
				String message = (String)Language.getWord("contrainte_be_connected",langue);
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("unaccessible.jsp");
				try {
					rd.forward(request, response);
				} catch (ServletException e) {
					e.printStackTrace();
				}			
			} 
		%>
		<% 
		if (request.getSession().getAttribute("user") != null) {
			User u = (User) request.getSession().getAttribute("user"); 
			if (u.getRole() == Role.SONDEUR) {
				String message = (String)Language.getWord("observer-sondage_survey_create",langue);
				out.print("<p>"+message+"</p>");
				List<Survey> s = (List<Survey>) PMF.getAllFromClass(Survey.class);
				String url2="resultats_display.jsp";
				for (Survey survey : s) {
					if (survey.getSondeurKey().equals(u.getKey())) {
						out.print("<li><a href=\"/"+ url2 +"?survey="+survey.getKey().getId()+"\">"+ survey.getName() +"</a></li>");
							
					}
				}			
			} else {
				String message = (String)Language.getWord("observer-sondage_survey_participated",langue);
				out.print("<p>"+message+"</p>");
				List<ResponseManager> rm = (List<ResponseManager>) PMF.getAllFromClass(ResponseManager.class);
				String url2="resultats_display.jsp";
				for (ResponseManager res : rm) {
					if ((res.getUserKey().equals(u.getKey())) && (res.hasAllreadyAnswered())) {
						String query = "select from " + Survey.class.getName()
							  + " where Key=" + res.getSurveyKey();
						Survey s = (Survey) PMF.executeQuery(query);
						out.print("<li><a href=\"/"+ url2 +"?survey="+s.getKey().getId()+"\">"+ s.getName() +"</a></li>");
							
					}
				}	
			}
		}
		%>
		
		<div class="clear"></div>
	</div>
	<div id="pied">
		<%@ include file="/layout/pied.tag" %>
	</div>
</div>

</body>
</html>